<?php
namespace app\store\controller\food\market\coupon;

use app\store\controller\food\Controller;
use app\store\model\food\CouponOffline as CouponOfflineModel;
use app\store\model\food\Coupon as CouponModel;
use app\store\model\food\Shop as ShopModel;
use think\facade\View;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

/**
 * 线下发券记录控制器
 */
class Offline extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
        $model = new CouponOfflineModel;
        $list = $model->getList();
        return View::fetch('index', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isGet()) {
            $model = new CouponModel;
            $list = $model->getList();
            $model = new ShopModel;
            $shoplist = $model->getList(false);
            return $this->renderSuccess('', '', compact('list','shoplist'));
        }
        $model = new CouponOfflineModel;
        if ($model->add($this->postData('data'))) {
            return $this->renderSuccess('添加成功', url('food.market.coupon.offline/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = CouponOfflineModel::get($id);
        if ($model->remove()) {
           return $this->renderSuccess('删除成功'); 
        }
		$error = $model->getError() ?: '删除失败';
        return $this->renderError($error);
        
    }
    
    /**
     * 二维码
     */
    public function qrcode($id)
    {
        $model = CouponOfflineModel::get($id,['coupon']);
        if($url = $model->qrcode()) {
            return redirect($url);
        }
		$error = $model->getError() ?: '二维码获取失败';
        return $this->renderError($error);
    }
    
    /**
     * 导出数据表
     */
    public function exports()
    {
        $data = $this->postData('data');
        //获取数据
        $model = new CouponOfflineModel;
        $list = $model->with(['coupon'])
            ->where('coupon_offline_id','>=',$data['star'])
            ->where('coupon_offline_id','<=',$data['end'])
            ->where('status',10)
            ->select();
        
        # 实例化 Spreadsheet 对象
        $spreadsheet = new Spreadsheet();
        # 获取活动工作薄
        $sheet = $spreadsheet->getActiveSheet();
        //文字左右居中对齐
        $styleArray = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        //黑色边框
        $borderStyleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN//细边框
                ],
            ],
        ];
        //设置单元格宽度
        $sheet->getColumnDimension('A')->setWidth(20);
        $sheet->getColumnDimension('B')->setWidth(20);
        $sheet->getColumnDimension('C')->setWidth(60);
        $sheet->setCellValue('A1','编号ID');
        $sheet->setCellValue('B1','券名称');
        $sheet->setCellValue('C1','二维码');
        $sheet->getStyle('A1:C1')->getFont()->setBold(true);//字体加粗
        $sheet->getStyle('A1:C1')->applyFromArray($styleArray);//水平居中

        $n = 1; //表格行数
        foreach ($list as $vo) {
            $n++;
            $sheet->getCell('A'.$n)->setValue((string)$vo['coupon_offline_id']);
            $sheet->getCell('B'.$n)->setValue($vo['coupon']['name']);
            $sheet->getCell('C'.$n)->setValue($vo['qrcode']);
            $sheet->getStyle('A'.$n.':C'.$n)->applyFromArray($styleArray);//水平居中
            $sheet->getStyle('A'.$n.':C'.$n)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);//垂直居中

        }
        $sheet->getStyle('A1:C'.$n)->applyFromArray($borderStyleArray);//添加边框 
        
        # Xlsx类 将电子表格保存到文件
        $writer = new Xlsx($spreadsheet);
        $file_name = $data['star'] . '-' .$data['end'] .'.xls';
        //$writer->save('temp/' . $file_name);//保存在本地
        
       // 客户端文件下载
        header('Content-Type:application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename='.$file_name);
        header('Cache-Control:max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
        $writer->save('php://output');
    }
}
